package durst.data.dao;

import java.sql.SQLException;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;

import durst.utils.ConfigParser;

public class MysqlDaoFactory extends DaoFactory {
	private JdbcPooledConnectionSource connectionSource = null; 
	
	protected MysqlDaoFactory() {
		try {
			connectionSource =  new JdbcPooledConnectionSource("jdbc:mysql://localhost/Calendar");
			connectionSource.setUsername(ConfigParser.getProp("durst.mysql.user"));
			connectionSource.setPassword(ConfigParser.getProp("durst.mysql.password"));
			connectionSource.setMaxConnectionAgeMillis(5 * 60 * 1000);
			connectionSource.setCheckConnectionsEveryMillis(60 * 1000);
			connectionSource.setTestBeforeGet(true);
			connectionSource.initialize();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public <D extends Dao<T,?>,T> D createDao(Class<T> clazz) throws SQLException {
		return createDao(connectionSource, clazz);
	}
	
	public JdbcPooledConnectionSource getConnectionSource() { 
		return connectionSource;
	}
}